<?php
$key            = '250619840406198327082011';
$strKey            = isset($_GET['k']) ? trim($_GET['k']) : null ;
$strCallback    = isset($_GET['callback']) ? trim($_GET['callback']) : null ;
$strSku            = isset($_GET['s']) ? trim($_GET['s']) : null ;
$intHourCur        = isset($_GET['h']) ? intval(trim($_GET['h'])) : date('G');
$aExtraTimeForOther = array(
    '2013-02-07' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-08' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-09' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-10' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-11' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-12' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-13' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-14' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-15' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-16' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-17' => array('instock'=>4*24, 'oos'=>6*24),
    '2013-02-18' => array('instock'=>4*24, 'oos'=>6*24),
);

$aExtraTimeForHanoiAndHCM = array(
    '2013-02-07' => array('added'=>5*24, 'threshold'=>0),
    '2013-02-08' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-09' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-10' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-11' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-12' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-13' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-14' => array('added'=>4*24, 'threshold'=>0),
    '2013-02-15' => array('added'=>3*24, 'threshold'=>0),
    '2013-02-16' => array('added'=>3*24, 'threshold'=>0),
    '2013-02-17' => array('added'=>3*24, 'threshold'=>0),
    '2013-02-18' => array('added'=>2*24, 'threshold'=>0),
    '2013-02-19' => array('added'=>1*24, 'threshold'=>0),
);


if($strCallback && $strSku && $strKey==$key)
{
    require_once 'CCommon.php';
    
    $obj        = new CCommon();
    $arrStock    = $obj->get_info_sku($strSku);
    
    /*if ($extraTime)
        $intResult  = $extraTime;
    else */
        $intResult    = 52;
    
    $instock = 0;
    #WMS - For Sale
    if( isset($arrStock[$strSku]['For Sale']) && intval($arrStock[$strSku]['For Sale'])>0 ){
        $intResult = 0;
        $instock = 1;
    }
    
    #Fullfiment Time
    $intResult+= 9;
    
    #Cs Time
    /*
    if($intHourCur+6>=18 && $intHourCur+6<=33){
        $intResult+=(24-$intHourCur)+9;
    }
    else{
        $intResult+=6;
    }
    */
    $intResult+=6;
    
    #Provine Time
    $arrProvince    = $obj->province_time();
    $strHtml        = '<select id="select-prov" name="select-prov">';
    foreach($arrProvince as $province=>$value)
    {
        #extra time
        $extraTime = 0;
        if (trim($province)=='Hà Nội' or trim($province)=='Hồ Chí Minh'){
            if ($instock==0)
                $extraTime = isset($aExtraTimeForHanoiAndHCM[date('Y-m-d')]['added']) ? $aExtraTimeForHanoiAndHCM[date('Y-m-d')]['added'] : 0;
        } else {
            if ($instock==0)
                $extraTime = isset($aExtraTimeForOther[date('Y-m-d')]['instock']) ? $aExtraTimeForOther[date('Y-m-d')]['instock'] : 0;
            else 
                $extraTime = isset($aExtraTimeForOther[date('Y-m-d')]['oos']) ? $aExtraTimeForOther[date('Y-m-d')]['oos'] : 0;
        }
        
        if (trim($province)=='Hà Nội' and $instock==1 and isset($aExtraTimeForHanoiAndHCM[date('Y-m-d')]['added'])){
            $extraTime = 1*24;
        }

        $temp = $intResult+$extraTime;
                
        $intDay        = ceil(($temp + intval(trim($value)))/24);
        /*if ( isset($aTimeInHolidays[date('Y-m-d')]['threshold']) and $intDay >= $aTimeInHolidays[date('Y-m-d')]['threshold'] )
            $intDay += $extraTime;*/
        if ($extraTime)
            $strHtml    = $strHtml.'<option value="'.($intDay-1).'-'.($intDay+1).'">'.trim($province).'</option>';
        else
            $strHtml    = $strHtml.'<option value="'.$intDay.'-'.($intDay+1).'">'.trim($province).'</option>';
    }
    $strHtml        = $strHtml.'</select>';
    echo $strCallback.'('.json_encode(array('html'=>$strHtml)).')';
}
?>